﻿namespace DataObject.Database
{
    using System.Data.Linq;
    using System.Data.Linq.Mapping;
    using System.Data;
    using System.Collections.Generic;
    using System.Reflection;
    using System.Linq;
    using System.Linq.Expressions;
    using System.ComponentModel;
    using System;
    using System.Configuration;

    public partial class OBSDataContext : System.Data.Linq.DataContext
    {
        public OBSDataContext() :
            base(System.Configuration.ConfigurationManager.ConnectionStrings["OBSConnectionString"].ConnectionString, mappingSource)
        {
            OnCreated();
        }
    }

    public partial class ServiceProvider
    {
        public string CurrencyName
        {
            get { return ((CurrencyEnum)Currency).GetStringValue(); }
        }
    }

    public partial class Route
    {
        public string Meals
        {
            get
            {
                if (NumOfMeals.HasValue)
                    return String.Concat(NumOfMeals.Value.Remove(3510).GetCountOfBitOne(), " breakfast(s), ",
                            NumOfMeals.Value.Remove(2925).GetCountOfBitOne(), " lunch(es), ",
                            NumOfMeals.Value.Remove(1755).GetCountOfBitOne(), " dinner(s), ");
                else
                    return null;
            }
        }

        public string RouteCode
        {
            get
            {
                string routeCode = RouteNumber.ToString();
                if (SubRouteCode.HasValue && SubRouteCode.Value != ' ')
                {
                    routeCode += "-" + SubRouteCode;
                }
                return routeCode;
            }
        }
    }

    public partial class Guest : INotifyPropertyChanging, INotifyPropertyChanged
    {
        public string FullName
        {
            get
            {
                return this.FirstName + " " + this.Lastname;
            }
        }

        private bool _hasBooking = false;
        public bool HasBooking
        {
            get { return _hasBooking; }
            set
            {
                if (_hasBooking != value)
                {
                    _hasBooking = value;
                }
            }
        }
    }

    public partial class Reservation : INotifyPropertyChanging, INotifyPropertyChanged
    {
        public DateTime EndDate
        {
            get
            {
                return CheckInDate.AddDays(NumOfNights);
            }
        }

        public decimal Balance
        {
            get { return SubTotal - Deposit - Paid; }
        }
    }

    public partial class ReservationDetail : INotifyPropertyChanging, INotifyPropertyChanged
    {
        public int BlockDates
        {
            get;
            set;
        }
    }

    public partial class ReservationService : INotifyPropertyChanging, INotifyPropertyChanged
    {
        public string CurrencyName
        {
            get { return ((CurrencyEnum)Currency).GetStringValue(); }
        }
        public string StatusString
        {
            get { return define.Status[Status]; }
        }
    }

    public partial class ViewOperatorReport
    {
        public string Shuttle
        {
            get
            {
                return IsShuttle ? "Yes" : String.Empty;
            }
        }

        public DateTime EndDate
        {
            get
            {
                return CheckInDate.AddDays(NumOfNights);
            }
        }

        public string CabinType
        {
            get
            {
                switch (Status)
                {
                    case 1:
                        return "Double";
                    case 2:
                        return "Twin";
                    case 3:
                        return "Single";
                    default:
                        return String.Empty;
                }
            }
        }

        public void FillPayment(IEnumerable<PaymentBatch> batches)
        {
            PaymentList = "<table style=\"width: 386px\">";
            foreach (var b in batches)
            {
                PaymentList += String.Format("<tr><td colspan=\"2\" style=\"border: solid 1px #000; background: #ffdf51\"><b>{0:MM/dd/yyyy}{2}</b></td><td style=\"border: solid 1px #000; background: #ffdf51\">{1:#,##0.##}</td></tr>", b.PaymentDate, b.PaidAmount, b.ReservationPayments.SingleOrDefault().IsDeposit ? "<i> (deposit)</i>" : String.Empty);
                foreach (var d in b.PaymentBatchDetails)
                {
                    PaymentList += String.Format("<tr><td style=\"border: solid 1px #000; background: #fff6cd\">{0}</td><td style=\"border: solid 1px #000; background: #fff6cd\">{1}</td><td style=\"border: solid 1px #000; background: #fff6cd\">{2:#,##0.##}</td></tr>",
                        ((PaidMethodEnum)d.PaymentMethod).GetStringValue(),
                        d.CurrencyAmount == null ? String.Empty : ((CurrencyEnum)d.CurrencyAmount.Value).GetStringValue(),
                        d.Amount);
                }
            }
            PaymentList += "</table>";
        }

        private string _paymentList = String.Empty;
        public string PaymentList
        {
            get { return _paymentList; }
            private set
            {
                if (_paymentList != value)
                {
                    _paymentList = value;
                }
            }
        }
    }

    public partial class ViewAccountantReport
    {
        public string Shuttle
        {
            get
            {
                return IsShuttle ? "Yes" : String.Empty;
            }
        }

        public string ClientType
        {
            get
            {
                return GuestType == 0 ? "Agent" : "WI";
            }
        }

        public string Service
        {
            get
            {
                return ExtraService > 0 ? "Yes" : String.Empty;
            }
        }

        public decimal Balance
        {
            get { return SubTotal - Deposit - Paid; }
        }

        public DateTime EndDate
        {
            get
            {
                return CheckInDate.AddDays(NumOfNights);
            }
        }
    }

    public partial class ViewPaymentReport
    {

        public string Shuttle
        {
            get
            {
                return IsShuttle.HasValue && IsShuttle.Value ? "Yes" : String.Empty;
            }
        }

        public string ClientType
        {
            get
            {
                return GuestType == 0 ? "Agent" : "WI";
            }
        }

        public string CurrencyAmountName
        {
            get { return CurrencyAmount.HasValue ? ((CurrencyEnum)CurrencyAmount.Value).GetStringValue() : String.Empty; }
        }

        public string CurrencyFeeName
        {
            get { return CurrencyFee.HasValue ? ((CurrencyEnum)CurrencyFee.Value).GetStringValue() : String.Empty; }
        }

        public string PaidMethodName
        {
            get { return PaymentMethod.HasValue ? ((PaidMethodEnum)PaymentMethod.Value).GetStringValue() : String.Empty; }
        }

        public decimal TotalAmt
        {
            get { return Amount.Value * Rate.Value; }
        }

    }

    public partial class ViewSummaryReport
    {
        public string Shuttle
        {
            get
            {
                return IsShuttle ? "Yes" : String.Empty;
            }
        }

        public string ClientType
        {
            get
            {
                return GuestType == 0 ? "Agent" : "WI";
            }
        }

        public string CurrencyAmountName
        {
            get { return CurrencyAmount.HasValue ? ((CurrencyEnum)CurrencyAmount.Value).GetStringValue() : String.Empty; }
        }

        public string CurrencyFeeName
        {
            get { return CurrencyFee.HasValue ? ((CurrencyEnum)CurrencyFee.Value).GetStringValue() : String.Empty; }
        }

        public string PaidMethodName
        {
            get { return PaymentMethod.HasValue ? ((CurrencyEnum)PaymentMethod.Value).GetStringValue() : String.Empty; }
        }

        public decimal TotalAmt
        {
            get
            {
                if (Amount.HasValue && Rate.HasValue)
                {
                    return Amount.Value * Rate.Value;
                }
                return 0;
            }
        }
    }

    public partial class ViewAccountReceivableHeader
    {
        public string Shuttle
        {
            get
            {
                return IsShuttle ? "Yes" : String.Empty;
            }
        }

        public string ClientType
        {
            get
            {
                return GuestType == 0 ? "Agent" : "WI";
            }
        }

        public string RouteID
        {
            get { return RouteNumber + (SubRouteCode.HasValue ? "-" + SubRouteCode.Value : String.Empty); }
        }

        public decimal Balance
        {
            get { return SubTotal - Deposit - Paid; }
        }

        public string ExSubTotalName
        {
            get { return String.Format("{0:#,##0.##} {1}", ExSubTotal, ((CurrencyEnum)CruiseCurrency).GetStringValue()); }
        }

        public string ExPaidName
        {
            get
            {
                if (CurrencyAmount.HasValue)
                    return String.Format("{0:#,##0.##} {1}", ExPaid, ((CurrencyEnum)CurrencyAmount.Value).GetStringValue());
                else
                    return null;
            }
        }

        public string ExBalanceName
        {
            get
            {
                decimal balance = ExBalance;
                if (CruiseCurrency == CurrencyAmount && balance != 0)
                    return String.Format("{0:#,##0.##} {1}", balance, ((CurrencyEnum)CruiseCurrency).GetStringValue());
                else
                    return null;
            }
        }

        public decimal ExBalance
        {
            get
            {
                if (ExSubTotal.HasValue && ExPaid.HasValue)
                    return ExSubTotal.Value - ExPaid.Value;
                else if (ExSubTotal.HasValue)
                    return ExSubTotal.Value;
                else if (ExPaid.HasValue)
                    return -ExPaid.Value;
                else
                    return 0;
            }
        }

        public DateTime EndDate
        {
            get
            {
                return DepartureDate.AddDays(NumOfNights);
            }
        }
    }

    public partial class ViewAccountPayableDetail
    {
        public string CurrencyName
        {
            get { return ((CurrencyEnum)Currency).GetStringValue(); }
        }

        public decimal Total
        {
            get { return Amount * Rate.Value; }
        }
    }

    public partial class ViewServicesReport
    {
        public string ServiceTypeName
        {
            get { return define.Services[ServiceType]; }
        }

        public string CurrencyName
        {
            get { return ((CurrencyEnum)Currency).GetStringValue(); }
        }

        private decimal _meals = -1;
        public decimal Meals
        {
            get
            {
                if (_meals == -1)
                {
                    if (ServiceType == 0)
                    {
                        _meals = ((int)Rate.Value).GetCountOfBitOne();
                        bool hasShuttle = (PackagePrice == 1);
                        bool hasFlight = (UnitPrice == 1);
                        if (null == ProviderName &&
                            !hasFlight && (!hasShuttle ||
                            (hasShuttle && (!IsShuttle.HasValue || !IsShuttle.Value))))
                        {
                            int lastBit;
                            NumOfMeals.Value.GetCountOfBitOne(out lastBit);
                            if (((int)Rate.Value & lastBit) == lastBit)
                            {
                                _meals--;
                            }
                        }
                    }
                    else
                    {
                        _meals = Rate.Value;
                    }
                }

                return _meals;
            }
        }

        public decimal Total
        {
            get
            {
                if (ServiceType == 0)
                {
                    decimal amount = Amount.HasValue ? Amount.Value : 0;
                    return amount * Meals * NumOfPax.Value;
                }
                else
                {
                    return Amount.Value * Rate.Value;
                }
            }
        }
    }

    public partial class ViewReservation
    {
        public bool DeleteHidden
        {
            get { return this._Status.Equals(1) || this._CheckInDate < DateTime.Now; }
        }
    }

    public partial class PaymentBatch
    {
        // Use binding, not store to DB
        public bool DepositFlag
        {
            get;
            set;
        }
    }

    public partial class CabinStatus
    {

        #region BoatID Property
        private string _boatId;

        /// <summary>
        /// Boat ID Property
        /// </summary>
        public string BoatID
        {
            get { return _boatId; }
            set
            {
                if (_boatId != value)
                {
                    _boatId = value;
                }
            }
        }
        #endregion

        #region BoatColor Property
        private string _boatColor = String.Empty;
        public string BoatColor
        {
            get { return _boatColor; }
            set
            {
                if (_boatColor != value)
                {
                    _boatColor = value;
                }
            }
        }
        #endregion

        #region Boat Name Property
        private string _boatName;

        /// <summary>
        /// Boat Name Property
        /// </summary>
        public string BoatName
        {
            get { return _boatName; }
            set
            {
                if (_boatName != value)
                {
                    _boatName = value;
                }
            }
        }
        #endregion

        #region CabinID Property
        private int _cabinID;
        public int CabinID
        {
            get { return _cabinID; }
            set
            {
                if (_cabinID != value)
                {
                    _cabinID = value;
                }
            }
        }
        #endregion

        #region Cabin Number Property
        private string _cabinNumber;

        /// <summary>
        /// Cabin Number Property
        /// </summary>
        public string CabinNumber
        {
            get { return _cabinNumber; }
            set
            {
                if (_cabinNumber != value)
                {
                    _cabinNumber = value;
                }
            }
        }
        #endregion

        #region Booking Property
        private BookingClass _booking;

        /// <summary>
        /// Booking Object Property
        /// </summary>
        public BookingClass Booking
        {
            get { return _booking; }
            set
            {
                if (_booking != value)
                {
                    _booking = value;
                }
            }
        }
        #endregion

        #region Departure Date Property
        private System.DateTime _departureDate;

        /// <summary>
        /// Departure Date Property
        /// </summary>
        public System.DateTime DepartureDate
        {
            get { return _departureDate; }
            set
            {
                if (_departureDate != value)
                {
                    _departureDate = value;
                }
            }
        }
        #endregion

        #region Status Property
        private int _status;

        /// <summary>
        /// Status Reservation
        /// </summary>
        public int Status
        {
            get { return _status; }
            set
            {
                if (_status != value)
                {
                    _status = value;
                }
            }
        }
        #endregion

    }

    public partial class BookingClass
    {

        public Guid ReservationID { get; set; }

        public char? SubRouteCode { get; set; }

        public int NumOfCabins { get; set; }

        public int NumOfNights { get; set; }

        public int NumOfPax { get; set; }

        public int Status { get; set; }

        public bool Pickup { get; set; }

        public bool IsShuttle { get; set; }

        public string RouteName { get; set; }

        public string Note { get; set; }

        public string BookingCode { get; set; }

        public string AgentID { get; set; }

        public string HotelAddress { get; set; }

        public string CssClass { get; set; }

        public string CreateByID { get; set; }

        public string ModifiedByID { get; set; }

        public string CabinTypes { get; set; }


        public DateTime DepartureDate { get; set; }

        public DateTime CurrentDate { get; set; }

        public DateTime CreationDate { get; set; }

        public DateTime ModifiedDate { get; set; }

        public DateTime EndDate
        {
            get
            {
                return DepartureDate.AddDays(NumOfNights);
            }
        }

        public string StringPickup
        {
            get { return (Pickup ? String.Concat("Pickup HCMC <input type='checkbox' checked/><p> Hotel Address: ", HotelAddress, "</p>") : String.Empty); }
        }

        public string StringShuttle
        {
            get { return (IsShuttle ? "Shuttle <input type='checkbox' checked/>" : String.Empty); }
        }

        public string DepartureDateString
        {
            get
            {
                return "Dep.: " + DepartureDate.ToShortDate();
            }
        }

        public string EndDateString
        {
            get
            {
                return "End : " + EndDate.ToShortDateString();
            }
        }

        public string Tooltip
        {
            get
            {
                string tooltip = String.Empty;
                tooltip = String.Format("<div style='background: white; color: black'><p>Booking Code: {0} </p><p>Agent#/WI: {1} </p><p>Route {2} </p>" +
                    "<p>N&#186; of nights: {3} </p><p>N&#186; of cabins: {4} </p>" +
                    "<p> {5} </p><p> {6} </p><p> {7} </p><p> {8} </p><p> {9} </p></div>",
                    BookingCode, AgentID, RouteName, NumOfNights, NumOfCabins, CabinTypes, StringPickup, StringShuttle, Note, NumOfPax);

                return tooltip;
            }
        }



        public string BackButton
        {
            get
            {
                return (this != null && CurrentDate.Date > DepartureDate.Date) ? "block" : "none";
            }
        }

        public string ForwardButton
        {
            get
            {
                return (this != null && CurrentDate.Date < EndDate.Date.AddDays(-1)) ? "block" : "none";
            }
        }

    }

    public partial class Appointment
    {

        #region BoatID Property
        private string _boatId = String.Empty;
        public string BoatID
        {
            get { return _boatId; }
            set
            {
                if (_boatId != value)
                {
                    _boatId = value;
                }
            }
        }
        #endregion

        #region BoatColor Property
        private string _boatColor = String.Empty;
        public string BoatColor
        {
            get { return _boatColor; }
            set
            {
                if (_boatColor != value)
                {
                    _boatColor = value;
                }
            }
        }
        #endregion

        #region CabinID Property
        private int _cabinID;
        public int CabinID
        {
            get { return _cabinID; }
            set
            {
                if (_cabinID != value)
                {
                    _cabinID = value;
                }
            }
        }
        #endregion

        #region Cabin Number Property
        private string _cabinNumber;
        public string CabinNumber
        {
            get { return _cabinNumber; }
            set
            {
                if (_cabinNumber != value)
                {
                    _cabinNumber = value;
                }
            }
        }
        #endregion

        #region Monday Property
        private BookingClass _monday;
        public BookingClass Monday
        {
            get { return _monday; }
            set
            {
                if (_monday != value)
                {
                    _monday = value;
                }
            }
        }
        #endregion

        #region Tuesday Property
        private BookingClass _tuesday;
        public BookingClass Tuesday
        {
            get { return _tuesday; }
            set
            {
                if (_tuesday != value)
                {
                    _tuesday = value;
                }
            }
        }
        #endregion

        #region Wednesday Property
        private BookingClass _wednesday;
        public BookingClass Wednesday
        {
            get { return _wednesday; }
            set
            {
                if (_wednesday != value)
                {
                    _wednesday = value;
                }
            }
        }
        #endregion

        #region Thursday Property
        private BookingClass _thursday;
        public BookingClass Thursday
        {
            get { return _thursday; }
            set
            {
                if (_thursday != value)
                {
                    _thursday = value;
                }
            }
        }
        #endregion

        #region Friday Property
        private BookingClass _friday;
        public BookingClass Friday
        {
            get { return _friday; }
            set
            {
                if (_friday != value)
                {
                    _friday = value;
                }
            }
        }
        #endregion

        #region Saturday Property
        private BookingClass _saturday;
        public BookingClass Saturday
        {
            get { return _saturday; }
            set
            {
                if (_saturday != value)
                {
                    _saturday = value;
                }
            }
        }
        #endregion

        #region Sunday Property
        private BookingClass _sunday;
        public BookingClass Sunday
        {
            get { return _sunday; }
            set
            {
                if (_sunday != value)
                {
                    _sunday = value;
                }
            }
        }
        #endregion

    }
}

